package cn.copera.dyy.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.copera.dyy.common.utils.StringUtils;
import cn.copera.dyy.common.web.BaseController;
import cn.copera.dyy.entity.Shop;
import cn.copera.dyy.entity.ShopImage;
import cn.copera.dyy.entity.Version;
import cn.copera.dyy.service.ShopImageService;
import cn.copera.dyy.service.ShopService;
import cn.copera.dyy.service.TrackService;
import cn.copera.dyy.service.VersionService;

/**
 * 手机端Controller
 * 
 * 
 */
@Controller
@RequestMapping(value = "${adminPath}/mobile")
public class ShopController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	ShopService shopService;
	@Autowired
	private TrackService trackService;
	@Autowired
	private ShopImageService shopImageService;
	@Autowired
	private VersionService versionService;
	
	@RequestMapping(value="/save",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> save(@RequestParam(required = false) String id,String shopName){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(id);
			shopName = changeToWord(shopName);
			if(shopName.indexOf("zhwhg") != -1){
				shopName = shopName.replace("zhwhg", "—");
			}
			shop.setShopName(shopName);
			shopService.save(shop);
			map = shopService.count(shop.getLoginName());
			map.put("message", "保存成功");
			map.put("shop", shopService.get(id));
		} catch (Exception e) {
			map.put("message", "保存失败，请输入合法字符");
		}
		return map;

	}	
	@RequestMapping(value="/saveHeadImage",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> saveHeadImage(@RequestParam(required = false) String shopId,String path){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(shopId);
			shopImageService.deleteImage(shopId);
			ShopImage shopImage = new ShopImage();
			shopImage.setShop(shop);
			
			
			shopImage.setFlag("shop_img");
			
			shopImage.setFileName(path);
			
			shopImageService.save(shopImage);
			
			map.put("message", "success");
			
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}	
	@RequestMapping(value="/saveSetImage",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveSetImage(@RequestParam(required = false) String shopId,String path,String description,String type,String width,String height,String id){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(shopId);
			ShopImage shopImage = null;
			if(StringUtils.isNotBlank(id)){
				shopImage=shopImageService.getShopImage(id);
			}else{
				 shopImage = new ShopImage();
			}
			
			shopImage.setShop(shop);
			shopImage.setDescription(description);
			shopImage.setType(type);
			shopImage.setFlag("set");
			shopImage.setWidth(width);
			shopImage.setHeight(height);
			shopImage.setFileName(path);
			
			shopImageService.save(shopImage);
			
			map.put("message", "success");
			
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}
	@RequestMapping(value="/checkVersion",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> checkVersion(){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Version v = versionService.findVersion();
			if(v!=null){
				map.put("version", v);
			}
			
			
			
		} catch (Exception e) {
			
		}
		return map;

	}	
	@RequestMapping(value="/saveAddress",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> saveAddress(@RequestParam(required = false) String id,String shopAddress){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(id);
			shopAddress = changeToWord(shopAddress);
			if(shopAddress.indexOf("zhwhg") != -1){
				shopAddress = shopAddress.replace("zhwhg", "—");
			}
			shop.setAddress(shopAddress);
			shopService.save(shop);
			map = shopService.count(shop.getLoginName());
			map.put("message", "保存成功");
			map.put("shop", shopService.get(id));
			map.put("trackToday", trackService.count4Today(shop.getLoginName()));
		} catch (Exception e) {
			map.put("message", "保存失败，请输入合法字符");
		}
		return map;

	}	
	
	@RequestMapping(value="/savePhone",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> savePhone(@RequestParam(required = false) String id,String shopPhone){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(id);
			shop.setPhone(shopPhone);
			shopService.save(shop);
			map = shopService.count(shop.getLoginName());
			map.put("message", "保存成功");
			map.put("shop", shopService.get(id));
			map.put("trackToday", trackService.count4Today(shop.getLoginName()));
		} catch (Exception e) {
			map.put("message", "保存失败，请输入合法字符");
		}
		return map;

	}	

	@RequestMapping(value="/savePass",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> savePass(@RequestParam(required = false) String id,String shopPass,String Pass1,String Pass2){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			Shop shop = shopService.get(id);
			if(shopPass.equals(shop.getPassword()) && Pass1.equals(Pass2)){
				shop.setPassword(Pass2);
				shopService.save(shop);
				map = shopService.count(shop.getLoginName());
				map.put("message", "保存成功");
				map.put("shop", shopService.get(id));
				map.put("trackToday", trackService.count4Today(shop.getLoginName()));
			}else if(!shopPass.equals(shop.getPassword())){
				map.put("message", "旧密码有误");
			}else if(!Pass1.equals(Pass2)){
				map.put("message", "重复密码不一致");
			}
		} catch (Exception e) {
			map.put("message", "保存失败，请输入合法字符");
		}
		return map;

	}	
	@RequestMapping(value="/saveUser",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> saveUser(@RequestParam(required = false) String id,String userName,String userPass){
		Map<String, Object> map = new HashMap<String,Object>();
		int count = shopService.getUserCount(userName);
		if(count==0){
			List<Shop> list =shopService.findById(id);
			Shop shop1 = shopService.get(id);
			
			try {
				if(list.size()<3){
					Shop shop = new Shop();
					shop.setParent(shop1);
					shop.setPhone(shop1.getPhone());
					shop.setLoginName(userName);
					shop.setPassword(userPass);
					shop.setValidDate("6");
					shopService.save(shop);	
					list.add(shop);
					map = shopService.count(shop.getLoginName());
					map.put("list", list);
					map.put("message", "保存成功");
				}else{
					map.put("message", "最多只能添加三个二级用户");
				}
			} catch (Exception e) {
				map.put("message", "保存失败，请输入合法字符");
			}
		}else{
			map.put("message", "账号已存在");
		}
		
		return map;
	}	

	@RequestMapping(value="/listUser",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listUser(@RequestParam(required = false) String id){
		Map<String, Object> map = new HashMap<String,Object>();
		List<Shop> list =shopService.findById(id);
				map.put("list", list);
		return map;
	}	

	@RequestMapping(value = "/shoplist", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> deptlist() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Shop> list =shopService.findAll();
		map.put("list", list);
		return map;
	}
	@RequestMapping(value="/blistUser",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> blistUser(@RequestParam(required = false) String id){
		Map<String, Object> map = new HashMap<String,Object>();
		List<Shop> list =shopService.findById(id);
				map.put("list", list);
				map.put("size", list.size());
		return map;
	}
	
	@RequestMapping(value="/deleteUser",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> deleteUser(@RequestParam(required = false) String userId,String shopId){
		shopService.deleteById(userId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Shop> list =shopService.findById(shopId);
		
				map.put("list", list);
				map.put("message", "删除成功");
		return map;
	}
	@RequestMapping(value="/findShopImg",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findShopImg(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
	    map.put("shopImage", shopService.findShopImg(shopId));
		return map;
	}
	@RequestMapping(value="/findSetImg",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findSetImg(@RequestParam(required = false) String shopId){
		Map<String, Object> map = new HashMap<String,Object>();
	    map.put("list", shopService.findSetImg(shopId));
		return map;
	}
	//设置卡点有效期
	@RequestMapping(value="/validDate",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> validDate(@RequestParam(required = false) String shopId,String validDate){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			shopService.validDate(shopId, validDate);
		    map.put("message", "设置成功");
		} catch (Exception e) {
			map.put("message", "设置失败");
		}
		return map;
	}
	
	/**
	 * 将获取的Unicode字符串解码成中文字
	 */
	public static String changeToWord(String str) {
		String retData = null;
		String tempStr = new String(str);
		String[] chStr = new String[str.length()/4];
		for(int i=0;i<str.length();i++){
			if(i%4==3){
				chStr[i/4] = new String(tempStr.substring(0, 4));
				tempStr = tempStr.substring(4, tempStr.length());
			}
		}
		char[] retChar = new char[chStr.length];
		for(int i=0;i<chStr.length;i++){
			retChar[i] = (char) Integer.parseInt(chStr[i], 16);
		}
		retData = String.valueOf(retChar, 0, retChar.length);
		return retData;
	}
	
	//获取店铺点赞和分享
	@RequestMapping(value="/getPraiseOrShare",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getPraiseOrShare(@RequestParam(required = false) String shopId){
		
		return shopService.getPraiseOrShare(shopId);
	}
	
	//编辑店铺宣传图片
	@RequestMapping(value="/editShopImage",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editShopImage(@RequestParam(required = false) String id,String name,String describe){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			ShopImage shopImage = shopImageService.getShopImage(id);
			if (shopImage != null) {
				shopImage.setDescription(describe);
				shopImage.setFileName(name);
				shopImageService.update(shopImage);
			}
			map.put("message", "编辑成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("message", "编辑失败");
		}
		return map;
	}
	
	//删除店铺宣传图片
	@RequestMapping(value="/deleteShopImage",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteShopImage(@RequestParam(required = false) String id){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			ShopImage shopImage = shopImageService.getShopImage(id);
			if (shopImage != null) {
				shopImage.setFileName(null);
				shopImageService.update(shopImage);
			}
			map.put("message", "删除图片成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("message", "删除图片失败");
		}
		return map;
	}
}
